package com.bezdelnick.forum.dao.hibernate;

import com.bezdelnick.forum.dao.ISubtypeDao;
import com.bezdelnick.forum.domain.Subtype;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 12:44
 * To change this template use File | Settings | File Templates.
 */
public class SubtypeDaoImpl implements ISubtypeDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(SubtypeDaoImpl.class);

    @Autowired(required = true)
    private SessionFactory sessionFactory;

    @Override
    public List<Subtype> loadSubtypes() {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Subtype.class).list();
        } catch (Error error) {
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public Subtype getSubtypeById(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Subtype subtype = (Subtype) session.get(Subtype.class, id);
            return subtype;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public Subtype getSubtypeByName(String name) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return (Subtype) session.createCriteria(Subtype.class).add(Property.forName("name").eq(name)).list().get(0);
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Subtype> loadSubtypesByTypeId(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Subtype.class).add(Property.forName("typeId").eq(id)).list();
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean insertSubtype(Subtype subtype) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.save(subtype);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean updateSubtype(Subtype subtype) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.update(subtype);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean deleteSubtype(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Subtype subtype = new Subtype();
            subtype.setId(id);
            session.delete(subtype);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }
}
